package com.blog.admin.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.blog.admin.bean.User;
import com.blog.admin.dao.MenuMapper;
import com.blog.admin.dao.RoleMapper;
import com.blog.admin.dao.UserMapper;
import com.blog.admin.service.UserService;
import com.github.pagehelper.PageHelper;

@Service
@Transactional
public class UserServiceImpl implements UserService
{
	@Resource
	UserMapper userMapper;
	
	@Resource
	MenuMapper menuMapper;
	
	@Resource
	RoleMapper roleMapper;

	public User queryUserByUserNameAndPassword(User user) 
	{	
		User loginUser = userMapper.selectUserByUserNameAndPassword(user);
		return loginUser;
	}

	public List<User> queryUserByOption(User user) {
		return userMapper.selectUserByOption(user);
	}

	
	public Integer addUser(User user,String[] roleId) throws Exception {
		List<User> userList = userMapper.selectUserByOption(user);
		if(userList.size()==0)
		{
			userMapper.insertUser(user);
			if(roleId!=null)
			{
				roleMapper.insertUserRoleMapping(user.getId(),roleId);	
			}
		}
		else
		{
			throw new Exception("用户名已经重复！");
		}
		return 1;
		
	}

	public Integer modifyUser(User user, String[] roleId) 
	{
		userMapper.updateUserById(user);
		roleMapper.deleteUserRoleMapingByUserId(user.getId());
		if(roleId!=null)
		{
			roleMapper.insertUserRoleMapping(user.getId(),roleId);	
		}
		return 1;
	}

	public Integer removeUserByUserId(String userId) 
	{
		roleMapper.deleteUserRoleMapingByUserId(Integer.parseInt(userId));
		userMapper.deleteUserById(userId);
		return 1;
	}

	public User queryUserByUserName(User user) {
		return userMapper.selectUserByUserName(user);
	}
	

}
